import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { ApiOperation, ApiTags, ApiBearerAuth } from '@nestjs/swagger'
import { AuthLocalGuard } from '../modules/local/guards/auth-local.guard';
import { AuthLocalDto } from '../modules/local/dtos/auth-local.dto'
import { CurrentUser } from '../decorators/current.decorator'
import { CommandBus } from '@nestjs/cqrs';
import { UserEntity } from '@/user/entities/user.mysql.entity';
import { CreateJwtCommand } from '../modules/jwt/commands/create-jwt.command'
import { AuthJwtGuard } from '../modules/jwt/guards/auth-jwt.guard'
@Controller('auth')
@ApiTags("鉴权")
export class AuthController {
  constructor(
    private readonly commandBus: CommandBus
  ) { }

  @ApiOperation({ summary: '用户登录' })
  @UseGuards(AuthLocalGuard)
  @Post('login')
  async login(@Body() body: AuthLocalDto, @CurrentUser() user: UserEntity) {
    const { id, username } = user
    const token = await this.commandBus.execute(new CreateJwtCommand({ id, username }))

    return {
      id,
      username,
      token
    }
  }

  @ApiOperation({ summary: 'jwt鉴权' })
  @ApiBearerAuth()
  @UseGuards(AuthJwtGuard)
  @Post('validate')
  async validateJwt(@CurrentUser() user: UserEntity) {
    return user
  }
}
